package com.haizhen.horizontalscrollslideview;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MainActivity2 extends AppCompatActivity {

    OverScrollLayout osl;
    RecyclerView rv;
    MovieAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rv = findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        rv.setAdapter(adapter = new MovieAdapter());
        //helper.attachToRecyclerView(rv);

        osl = findViewById(R.id.osl);
        osl.setOnOverScrollReleaseListener(new OverScrollLayout.OnOverScrollReleaseListener() {
            @Override
            public void onRelease() {
                Toast.makeText(MainActivity2.this, "回调", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.MovieHolder> {

        List<Integer> data = new ArrayList<>();

        MovieAdapter() {
            data.add(R.drawable.post1);
            data.add(R.drawable.post2);
            data.add(R.drawable.post3);
            data.add(R.drawable.post4);
            data.add(R.drawable.post5);
        }

        @NonNull
        @Override
        public MovieHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_movie, viewGroup, false);
            return new MovieHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull MovieHolder movieHolder, int i) {
            movieHolder.iv.setImageResource(data.get(i));
        }

        @Override
        public int getItemCount() {
            return data.size();
        }

        class MovieHolder extends RecyclerView.ViewHolder {
            ImageView iv;

            MovieHolder(@NonNull View itemView) {
                super(itemView);
                iv = itemView.findViewById(R.id.iv_movie);
                iv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity2.this, "item点击", Toast.LENGTH_SHORT).show();
                    }
                });
//                iv.setOnLongClickListener(new View.OnLongClickListener() {
//                    @Override
//                    public boolean onLongClick(View v) {
//                        Toast.makeText(MainActivity.this, "长按", Toast.LENGTH_SHORT).show();
//                        return true;
//                    }
//                });
            }
        }
    }
}
